<?php
//session_start();
/**
** $Author: serge $
** $Revision: 1.7 $
** $Date: 2006/08/11 15:51:31 $
** $Log: CalAgenda.php,v $
** Revision 1.7  2006/08/11 15:51:31  serge
** Preversion, juste avant la premiere phase de test.
**
*/

/****************************************************************
** Application : Nova
**
** Les buts de cet application sont de fournir un outils de partage
** de l'information pour le site membre "nova"
**
** Les auteurs :
** 		Design Web 			: Serge Peter
** 		Developpement PHP 	: Serge Peter
** 		Etudes 				: Sandrine Guihenneuc
**		Feuille de style	: Sandrine Guihenneuc
** Fichier CalAgenda.php :
** 		Ce fichier regroupe les fonctions speciiques au calendrier
**
*****************************************************************/

/*
** Function : print_cal
**
** Deal : print g&eacute;n&eacute;ral du calendrier
**
** Params : database connection, based form name, service id.
*/
function print_cal ($db,  $service_id )
{

	/* lecture des variable */
	
	if (isset($_GET["n_day"]))
		$n_day = $_GET['n_day'];
	else 
		$n_day="";
		
	if (isset($_GET["n_month"]))
		$n_month = $_GET['n_month'];
	else 
		$n_month="";		
	
	if (isset($_GET["n_year"]))
		$n_year = $_GET['n_year'];
	else 
		$n_year="";
		
	if (isset($_GET["cal_mode"]))
		$cal_mode = $_GET['cal_mode'];
	else
		$cal_mode = "";


	/* initialisation des variables     */
	if ( $cal_mode == "" ) {
		$cal_mode=1;
	}

	if ( $n_day == "" ) {
	    $today = getdate();
	    $n_month = $today['mon'];
	    $n_day = $today['mday'];
	    $n_year = $today['year'];
	}

	/* traiter les different mode de visualisation */
	switch ($cal_mode)
	{
		case 0;		// mode edit
	    edit_cal($db,  $service_id);
	    print_cal_footer();
	    break;

		case 2;		// mode edit

		/* initialiser des variable sessions pour plus tard ...*/
		if ($_POST['date_debut'] != "") {
		$_SESSION['date_debut'] = $_POST['date_debut'];
		$_SESSION['date_fin'] = $_POST['date_fin'];
		$_SESSION['type_query'] = $_POST['type_query'];
		}

//        list_cal($db,  $service_id, $date_debut, $date_fin, $type_query);
        list_cal($db,  $service_id, 2);
	    print_cal_footer();
	    break;

		case 3;		// mode print preview

		/* initialiser des variable sessions pour plus tard ...*/
		if ($_POST['date_debut'] != "") {
		$_SESSION['date_debut'] = $_POST['date_debut'];
		$_SESSION['date_fin'] = $_POST['date_fin'];
		$_SESSION['type_query'] = $_POST['type_query'];
		}

        list_cal($db,  $service_id, 3);
	    print_cal_footer();
	    break;

		case 5;    // edit
	    edit_cal($db,  $service_id);
	    print_cal_footer();
	    break;

		case 6;    // edit
	    edit_cal($db,  $service_id);
	    print_cal_footer();
	    break;

		case 1;    // view
	    print_cal_header($db,  $service_id, $n_day, $n_month, $n_year);
	    print_cal_content($db,  $service_id, $n_day, $n_month, $n_year );
	    print_cal_footer();
	    break;

		case 1111;    // pdf
	    print_to_pdf_cal($db,  $service_id, $n_day, $n_month, $n_year);
	    break;


	    case 1000;	// mode effacer
	    edit_cal($db,  $service_id);
	    print_cal_footer();
	    break;

	    case 1001;	// mode effacer cyclique
	    edit_cal($db,  $service_id);
	    print_cal_footer();
	    break;
  }  // switch
}

/*
** Function : edit_cal
**
** Deal : appel&eacute;e pour editer le contenu d'un evenement
**
** Params : database connection, based form name, service id.
*/
function edit_cal($db,  $service_id)
{

	/* lecture des variable PUT/GET */

	if (isset($_GET["cal_id"]))
		$id = $_GET['cal_id'];
	else
		$id = "";
		
	if (isset($_GET["cal_mode"]))
		$action = $_GET['cal_mode'];
	else
		$action = "";
	
	if (isset($_GET["n_day"]))
		$n_day = $_GET['n_day'];
	else 
		$n_day="";
		
	if (isset($_GET["n_month"]))
		$n_month = $_GET['n_month'];
	else 
		$n_month="";		
	
	if (isset($_GET["n_year"]))
		$n_year = $_GET['n_year'];
	else 
		$n_year="";
		
	if (isset($_GET["edit"]))
		$edit = $_GET['edit'];
	else
		$edit = "";

	/* variable "globale" a la function*/
	$table_name = TB . 'evenements';
	$titre = "Evenements";
	$shortname="evenements";

    /* check des droits */
	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";


	/* application du mode par d&eacute;faut. */
	if (empty ($action))
	    $action = 0;   // edition

	/* travail en fonction des actions */
	switch($action)
	{

	    case 1000;   // Mode effacer

		/******************
			 Verification des droits
		*/
	    /* 
	     * definir la query 
		*/
	    $sql = 'select * from ' . $table_name . '
	    where id = "' . $id . '"';

	    /* lancer la query */
	    $res = $db->query($sql);

	    /* pas d'erreur base de donn&eacute;es */
	    /* assignation dans variables session */
	    if (is_array($row = $db->getRow($sql))) {
	        list(
	            $id,
	            $utilisateur_id
	        ) = $row;
	    }

	    if ($utilisateur_id != $_SESSION['user_id'] and $_SESSION['ponderation'] != 20){
		        $url=INDEX_FORM . "?id=" . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
				go_back($url,MSG_CAL_RIGHT_ERR,3);
		}else{

	        /******************
	             On fait le delete
	        */
	        /* definir la query */
	        $sql = 'delete from ' . $table_name .'
	            where id = "' . $id . '"' ;

	        /* lancer la query */
	        $res = $db->query($sql);

	        /* test des erreurs */
	        if ( DB::isError( $res = $db->query( $sql ) ) ) {
		        echo MSG_DB_ERR,DB::errorMessage($res), '<br/>';
	        }else{
	            /* tout s'est bien pass&eacute; */
		        $url=INDEX_FORM . "?id=" . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
				go_back($url,MSG_DEL_RECORD,1);

			} // test db
	    } // if : droit d'acces a l'info
	    break;

		case 1001;   // Mode effacer cyclique

		/******************
			 Verification des droits
		*/
	    /* Definir la query */
	    $sql = 'select * from ' . $table_name . '
	    where id = "' . $id . '"';

	    /* lancer la query */
	    $res = $db->query($sql);

	    /* pas d'erreur base de donn&eacute;es */
	    /* assignation dans variables session */
	    if (is_array($row = $db->getRow($sql))) {
	        list(
	            $id,
	            $utilisateur_id
	        ) = $row;
	    }

	    if ($utilisateur_id != $_SESSION['user_id'] and $_SESSION['ponderation'] != 20){

	        $url=INDEX_FORM . "?id=" . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
			go_back($url,MSG_CAL_RIGHT_ERR,3);

	    }else{

	        /******************
	             On fait le delete
	        */
	        /* definir la query */
	        $sql = 'delete from ' . $table_name .'
	            where id = "' . $id . '" or rep_parent_id = "' . $id . '"';

	        /* lancer la query */
	        $res = $db->query($sql);

	        /* test des erreurs */
	        if ( DB::isError( $res = $db->query( $sql ) ) ) {
		        echo MSG_DB_ERR,DB::errorMessage($res), '<br/>';
	        }else{

	            /* tout s'est bien passe */
		        $url=INDEX_FORM . "?id=" . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
				go_back($url,MSG_DEL_RECORD,1);

			} // test db
	    } // if : droit d'acc&eacute;s a l'info

	break;

    case 5;   // print to pdf
	print_evenement_to_pdf($db,  $service_id, $table_name, $id);
	break;

    case 6;   // print to pdf
	print_cal_list_to_pdf($db,  $service_id, $table_name, $id);
	break;

	case 0;  // mode ajouter ou editer

	    /* il s'agit d'un nouveau utilisateur , on essigne les valeurs par d&eacute;faut */
	    if ($id == "NA"){
            $utilisateur_id = $_SESSION['user_id'] ;
            $titre = null ;
            $description = null ;
            $ev_date = $n_year . "-" . $n_month . "-" . $n_day;
			$duree = "1:30" ;
            $audience = 0 ;
            $type_id = 0 ;
            $projet_id = 5 ;
            $prive = 0 ;
            $rep_periodique = 0;
            $rep_type = "Toutes les semaines";
            $rep_duree = "Pas de dur&eacute;e";
            $rep_patent_id = null;
            $rep_date_fin = $n_year . "-" . $n_month . "-" . $n_day;
			
			
			$tache_terminee="";
			$projet="";
			$treated_duree="";
			$treated_ev_date="";
			$treated_ev_heure="";
		

	    }else{

			/* definir la query */
	        $sql = 'SELECT ev.id,
					ev.utilisateur_id,
					ev.titre,
					ev.ev_date,
					ev.duree,
					ev.description,
					ev.audience,
					ev.type_id,
					ev.projet_id,
					ev.prive,
					ev.tache_terminee,
					ev.rep_periodique,
					ev.rep_type,
					ev.rep_duree,
					ev.rep_parent_id,
					ev.rep_date_fin,
	        		usr.username,
	        		ut.nom as utilisateur_type,
	        		pr.nom as project,
	        		et.nom as evenement_type,
					DATE_FORMAT(ev.duree, "%Hh%i") as treated_duree,
					DATE_FORMAT(ev.ev_date,"%d/%m/%Y") as treated_ev_date,
					DATE_FORMAT(ev.ev_date,"%Hh%i") as treated_ev_heure
	        		FROM ' . TB . 'evenements ev, ' . TB . 'evenement_types et, ' . TB . 'utilisateurs usr, ' . TB . 'utilisateur_types ut, ' . TB . 'projets pr
	        		WHERE ev.id =  ' . $id  . ' 
	        		AND usr.id = ev.utilisateur_id AND ut.id = ev.audience AND pr.id = ev.projet_id AND ev.type_id = et.id';

	        
	        /* lancer la query */
	        $res = $db->query($sql);

	        /* pas d'erreur base de donn&eacute;es */
	        /* assignation dans variables session */
	        if (is_array($row = $db->getRow($sql))) {
	            list(
				$id,
				$utilisateur_id,
				$titre,
				$ev_date,
				$duree,
				$description,
				$audience,
				$type_id,
				$projet_id,
				$prive,
				$tache_terminee,
				$rep_periodique,
				$rep_type,
				$rep_duree,
				$rep_parent_id,
				$rep_date_fin,
	            $username,
	            $utilisateur_type,
	            $projet,
	            $evenement_type,
				$treated_duree,
				$treated_ev_date,
				$treated_ev_heure
	        ) = $row;
			}
	    } // if sur mode ajout / edit

        /**************************
        	Attention est-ce qu'on a les droits d edition ???? sinon on montre l'info
        	En meme temps si on est en mode view (edit = "" ) on passe par la...
        */
	    if (($utilisateur_id != $_SESSION['user_id'] and $_SESSION['ponderation'] != 20 ) or $edit == ""  or $has_rights == "no" ){
	    	
	    	 init_table_display("Ev&eacute;nement", "info");

	    	 ?>
	    	 <div class="info">
	        <table style="text-align: left; width: 90%;" border="0" cellpadding="2" cellspacing="2">
	        <tbody>
	        <tr>
		        <th width="21%"><big>Titre</big></th>
				<th width="79%">
					<?php if ( $type_id == 4 ){
						if ( $tache_terminee == 0 ) {
							echo IMG_CAL_1;
							} else {
							echo IMG_CAL_2;
						}
					}
					echo $titre ;
		
					if ( $rep_periodique == 1 ){
					echo IMG_CAL_5;
					} ?>
		        </th>
	        </tr>
	        <tr>
		        <td>Description</td>
		        <td>
		        	<?php echo $description ;?>
		        </td>
	        </tr>
	        <tr>
	        	<td>Date</td>
	        	<td>
            		<?php echo $treated_ev_date;?>
            	</td>
	        </tr>
	        <tr>
	        	<td>Heure</td>
	        	<td>
            		<?php echo $treated_ev_heure;?>
            	</td>
	        </tr>
			<tr>
	        	<td>Dur&eacute;e</td>
	        	<td>
		    		<?php echo $treated_duree ;?>
		    	</td>
	        </tr>
	        <tr>
	        	<td>Cr&eacute;ateur</td>
	        	<td><?php echo $username ;?></td>
	        </tr>
	        <tr>
	        	<td>Audience</td>
	        	<td><?php echo $utilisateur_type ;?></td>
	        </tr>
	        <tr>
	        	<td>Type</td>
	        	<td><?php echo $evenement_type ;?></td>
	        </tr>
	        <tr>
	        	<td>Projet</td>
	        	<td><?php echo $projet ;?></td>
					<?php if ( $rep_periodique == 1 ){ ?>
				  	<tr>
						<td>R&eacute;p&eacute;tition</td>
						<td><?php echo $rep_type;?></td>
			  		</tr>
					<tr>
						<td>Jusqu'au</td>
						<td><?php echo $rep_date_fin ;?></td>
					</tr>
					<?php }else{?>
					<tr>
						<td>R&eacute;p&eacute;tition</td>
						<td>Aucune</td>
					</tr>
					<?php }?>
	        </tr>
	        </tbody>
			</table>
			</div>
	        
	        <p>
			<?php if ( $has_rights != "no" ){
					if ( $rep_periodique == 1 and  $rep_parent_id != null and $has_rights != "no" )
					{
						$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $rep_parent_id . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
						$label=MSG_CAL_1;
						print_url($url,$label);
					}else{
						$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $id . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
						$label=MSG_CAL_2;
						print_url($url,$label);
					}}

					echo "  ";

					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=5" . "&cal_id=" . $id . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
					$label="Imprimer";
					print_url($url,$label);
					echo " ";
					$url=INDEX_FORM . '?id=' . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
					$label=MSG_CAL_3;
					print_url($url,$label);
					?>
	        </p>
					
			<?php 

			 end_table_display();
			 
			}else{
	        /**************************
	            La on est en mode edit !!!!!
	        */

	        /*
	        ** cr&eacute;ation du formulaire a l'aide des fonction de HTML_QuickForm
	        */
	        $form = new HTML_QuickForm('frm' . $titre, 'post',  INDEX_FORM . '?id=' . $service_id . '&cal_mode=' . $action . '&cal_id=' . $id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year . '&edit=1');
	        $form->addElement('hidden', 'id', 'Id', array('value' => $id));
	        $form->addElement('hidden', 'utilisateur_id', 'Utilisateur id', array('value' => $utilisateur_id));
	        $form->addElement('text', 'titre', 'Titre', array('value' => $titre, 'size' => 60, 'maxlength' => 100));

	        $attrs = array("class"=>"warn",
	           "rows"=>"10",
	           "cols"=>"60",
			   "id" => "description");

	        $form->addElement('textarea', 'description', 'Description', $attrs);
			$form->setDefaults(array('description' => $description));

			$form->addElement('html', "
				<script type='text/javascript' src='js/toolbar.js'></script>
				<script type='text/javascript'>
				if (document.getElementById) {
				var tb = new dcToolBar(document.getElementById('description'),'html','images/');
				tb.btStrong('Forte emphase');
				tb.btEm('Emphase');
				tb.btIns('Ins&eacute;r&eacute;');
				tb.btDel('Supprim&eacute;');
				tb.btQ('Citation en ligne');
				tb.btCode('Code');
				tb.addSpace(10);
				tb.btBr('Saut de ligne');
				tb.addSpace(10);
				tb.btBquote('Bloc de citation');
				tb.btPre('Texte pr&eacute;format&eacute;');
				tb.btList('Liste non ordonn&eacute;e','ul');
				tb.btList('Liste ordonn&eacute;e','ol');
				tb.addSpace(10);
				tb.btLink('Lien',
				'URL ?',
				'Langue ?',
				'fr');
				tb.btImgLink('Image externe','URL ?');
				tb.addSpace(10);
				tb.draw('Vous pouvez utiliser les raccourcis suivants pour enrichir votre pr&eacute;sentation.');
				}
				</script>
				");

			 $options = array(
                'language'  => 'fr',
                'format'    => 'dMYHi',
                'minYear'   => 2004,
                'maxYear'   => 2010
                );
            $form->addElement('date', 'ev_date', 'Date et heure', $options);
	        $form->setDefaults(array('ev_date' => $ev_date));

			 $options = array(
                'language'  => 'fr',
                'format'    => 'Hi'
                );
            $form->addElement('date', 'duree', 'Dur&eacute;e', $options);
	        $form->setDefaults(array('duree' => $duree));

	        $res1 = $db->getAssoc('select id, nom from ' . TB . 'utilisateur_types order by ponderation');
	        $form->addElement('select', 'audience', 'Audience', $res1, 'id=id');
	        $form->setDefaults(array('audience' => $audience));

	        $res2 = $db->getAssoc('select id, nom from ' . TB . 'evenement_types order by nom');
	        $form->addElement('select', 'type_id', 'Type', $res2, 'id=id');
	        $form->setDefaults(array('type_id' => $type_id));

	        $res3 = $db->getAssoc('select id, nom from ' . TB . 'projets order by nom');
	        $form->addElement('select', 'projet_id', 'Projet', $res3, 'id=id');
	        $form->setDefaults(array('projet_id' => $projet_id));

	        $prive_a = array(
	                "0"=>"Non",
	                "1"=>"Oui"
	                 );

	        $form->addElement('select', 'prive', 'Priv&eacute;', $prive_a);
	        $form->setDefaults(array('prive' => $prive));

			$form->addElement('select', 'tache_terminee', 'T&acirc;che termin&eacute;e ?', $prive_a);
	        $form->setDefaults(array('tache_terminee' => $tache_terminee));

	        $form->addElement('select', 'rep_periodique', 'R&eacute;p&eacute;tition ?', $prive_a);
	        $form->setDefaults(array('rep_periodique' => $rep_periodique));


			$rep_type_a = array(
			"Tous les jours"=>"Tous les jours",
			"Toutes les semaines"=>"Toutes les semaines",
			"Toutes les 2 semaines"=>"Toutes les 2 semaines",
			"Toutes les 3 semaines"=>"Toutes les 3 semaines",
			"Toutes les 4 semaines"=>"Toutes les 4 semaines",
			"Toutes les 5 semaines"=>"Toutes les 5 semaines"
			);

		    $form->addElement('select', 'rep_type', 'R&eacute;peter :', $rep_type_a);
	        $form->setDefaults(array('rep_type' => $rep_type));

	        $options = array(
                'language'  => 'fr',
                'format'    => 'dMY',
                'minYear'   => 2004,
                'maxYear'   => 2010
                );
	        $form->addElement('date', 'rep_date_fin', "Jusqu'a :", $options);
	        $form->setDefaults(array('rep_date_fin' => $rep_date_fin));

	        $form->addRule('titre', 'Vous devez saisir un titre', 'required', '', 'client');
	        $form->addRule('ev_date', 'Vous devez saisir un jour', 'required', '', 'client');
	        $form->addRule('duree', 'Vous devez saisir une durr&eacute;e', 'required', '', 'client');
	        $form->addRule('audience', 'Vous devez saisir une audience', 'required', '', 'client');
	        $form->addRule('type_id', 'Vous devez saisir un type', 'required', '', 'client');


	        $form->applyFilter('titre','trim') ;
	        $form->setRequiredNote('<span style="color: #ff0000">*</span> = champs obligatoires');
	        $form->setJsWarnings('Erreur de saisie','Veuillez corriger');

	        $form->addElement('submit', 'bouton_effacer', 'Envoyer');

	        /* c'est quand l'utlisateur presse le bouton .... */
	        if ($form->validate()) {  //form

                $date_ar = $form->exportValue('ev_date');
                $date_str = $date_ar["Y"] . "-" . $date_ar["M"] . "-" . $date_ar["d"] . " " . $date_ar["H"] . ":" . $date_ar["i"];

                $duree_ar = $form->exportValue('duree');
                $duree_str = $duree_ar["H"] . ":" . $duree_ar["i"];

                $rep_ar = $form->exportValue('rep_date_fin');
                $rep_str = $rep_ar["Y"] . "-" . $rep_ar["M"] . "-" . $rep_ar["d"];

	            /* on r&eacute;cupere les valeurs du formulaire */
	            $fields_values = array(
	            'utilisateur_id' => $_SESSION['user_id'],
	            'titre' => $form->exportValue('titre'),
	            'description' => $form->exportValue('description'),
	            'ev_date' => $date_str,
	            'duree' => $duree_str,
	            'audience' => $form->exportValue('audience'),
	            'type_id' => $form->exportValue('type_id'),
	            'projet_id' => $form->exportValue('projet_id'),
	            'prive' => $form->exportValue('prive'),
	            'rep_periodique' => $form->exportValue('rep_periodique'),
 				'rep_type' => $form->exportValue('rep_type'),
				'rep_date_fin' => $rep_str,
	            'tache_terminee' => $form->exportValue('tache_terminee')
               );

	            /* test pour savoir si on fait un update ou un insert */
	            if ($id == "NA" ){
	                $resins = $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
	                $titre = $form->exportValue('titre');
	            $sql = 'SELECT id FROM ' . $table_name .
						' where titre = "' . $titre . '"' .
						' and ev_date <= "' . $date_str . '"';

		        /* pas d'erreur base de donn&eacute;es */
		        /* assignation dans variables session */
		        if (is_array($row = $db->getRow($sql))) {
		            list(
					$id) = $row;
		        }
	            }else{
	                $resins = $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_UPDATE, "id = $id");
	            }

	            if (DB::isError($resins)) {
					/* utilisation d'une fonction de print de message d'erreur */
					print_insupd_err(MSG_INSUPG_ERR, $resins->getMessage());
	 				form_display($form, "Edition d'un &eacute;v&eacute;nement", "forms", "100%");
	            }

				/* Traitement des p&eacute;riodicit&eacute;s ...*/
				/* a - efface ancien records */
				$rep_periodique = $form->exportValue('rep_periodique');

				if ($rep_periodique == "1"){
					echo "Effacer les anciens &eacute;v&eacute;nements periodiques<br/>";
					$sql = "delete from " . $table_name ." where rep_parent_id = " . $id .
					        " and id != " .$id;

				    /* lancer la query */
	        		$res = $db->query($sql);
	        		if (DB::isError($res)) {
	                echo "L'effacement a &eacute;chouer. Voici le message d'erreur de la base de donn&eacute;es : <br/>";
	                echo $res->getMessage();
	        		}

				/* b - ajouter les nouveaux */
				$rep_type = $form->exportValue('rep_type');
				echo "Ajouter les nouveaux &eacute;v&eacute;nements.<br/>";
				switch ($rep_type)
				{
				case "Tous les jours" ;
				$increment = 1;
				break;
				case "Toutes les semaines" ;
				$increment = 7;
				break;
				case "Toutes les 2 semaines" ;
				$increment = 14;
				break;
				case "Toutes les 3 semaines" ;
				$increment = 21;
				break;
				case "Toutes les 4 semaines" ;
				$increment = 28;
				break;
				case "Toutes les 5 semaines" ;
				$increment = 35;
				break;
				}
				$cur_time = mktime(0, 0, 0, $date_ar["M"], $date_ar["d"] , $date_ar["Y"]);
				$fin_time = mktime(0, 0, 0, $rep_ar["M"], $rep_ar["d"] , $rep_ar["Y"]);
				$cur_date = getdate($cur_time);
				$fin_date = getdate($fin_time);
				echo "Date de d&eacute;but : " . $cur_date['year'] . "-" . $cur_date['mon'] . "-" . $cur_date['mday']  . '<br/>';
				echo "Date de fin : " . $fin_date['year'] . "-" . $fin_date['mon'] . "-" . $fin_date['mday']  . '<br/>';
				echo "Incr&eacute;ment : " . $increment . " jours.<br/>";

				$cur_time = $cur_time+($increment*24*3600);
				$cur_date = getdate($cur_time);

				while (($cur_date['year'] * 10000 +  $cur_date['mon'] * 100 + $cur_date['mday'])  <= ($fin_date['year'] * 10000 + $fin_date['mon'] * 100 + $fin_date['mday'])  ) {

					// insertion des incr&eacute;ments
		            $fields_values = array(
		            'utilisateur_id' => $_SESSION['user_id'],
		            'titre' => $form->exportValue('titre'),
		            'description' => $form->exportValue('description'),
		            'ev_date' => $cur_date['year'] . "-" . $cur_date['mon'] . "-" . $cur_date['mday']. " " . $date_ar["H"] . ":" . $date_ar["i"],
		            'duree' => $duree_str,
		            'audience' => $form->exportValue('audience'),
		            'type_id' => $form->exportValue('type_id'),
		            'projet_id' => $form->exportValue('projet_id'),
		            'prive' => $form->exportValue('prive'),
		            'rep_periodique' => $form->exportValue('rep_periodique'),
	 				'rep_type' => $form->exportValue('rep_type'),
					'rep_date_fin' => $rep_str,
					'rep_parent_id' => $id
		             );

		            $cur_time = $cur_time+($increment*24*3600);
					$cur_date = getdate($cur_time);

	                $resins = $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
					}
					echo "Date courrante apr&eacute;s la boule : " . $cur_date['year'] . "-" . $cur_date['mon'] . "-" . $cur_date['mday']  . '<br/>';
				}

	            /* check des erreurs */
	            if (DB::isError($resins)) {
					/* utilisation d'une fonction de print de message d'erreur */
					print_insupd_err(MSG_INSUPG_ERR, $resins->getMessage());
	 				form_display($form, "Edition d'un &eacute;v&eacute;nement", "forms", "100%");
	            }else{
					$url=INDEX_FORM . "?id=" . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
					$label=MSG_INSUPD_RECORD;
					go_back($url,$label,1);
	            }

	        }else{
				form_display($form, "Edition d'un &eacute;v&eacute;nement", "forms", "100%");
				$url=INDEX_FORM . '?id=' . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
				$label=MSG_CAL_3;
                print_url($url,$label);
			} //form
		} // autorisation
	break;
	} //switch
}

/*
** Function : translate_month
**
** Deal : translation du num&eacute;ro de mois en francais
**
** Params : month => num&eacute;ro 1-12 ou texte anglais
*/
function list_cal ( $db,  $service_id, $cal_mode)
{

	/* check des droits */
	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";

	/* initialisation de la date du jour */
	$today = getdate();
	$today_month = $today['mon'];
	$today_day = $today['mday'];
	$today_year = $today['year'];

	if (isset($_GET["n_day"]))
		$n_day = $_GET['n_day'];
	else 
		$n_day="";
		
	if (isset($_GET["n_month"]))
		$n_month = $_GET['n_month'];
	else 
		$n_month="";		
	
	if (isset($_GET["n_year"]))
		$n_year = $_GET['n_year'];
	else 
		$n_year="";

	if ($today_day == $n_day and $today_month == $n_month and $today_year == $n_year){
		$today_color="rgb(182,213,234)";
	}else{
		$today_color="rgb(224, 224, 224)";
	}

	/* get session variables */
	$date_debut = $_SESSION['date_debut'];
	$date_fin = $_SESSION['date_fin'];
	$type_query = $_SESSION['type_query'];

	$title_begin="Liste des &eacute;v&eacute;nements et t&acirc;ches  pour la p&eacute;riode du ";

	/* grand switch, on construit les query en fonctions des demande du POST */
	switch ($type_query)
	{
	case "tout" ;
		/* d&eacute;finition de la query avec v&eacute;rification des droits */
		$sql = "SELECT ev.id , CONCAT( LEFT(ev.titre, 40) ,  '...'  )  AS cut_titre,  usr.username,
		DATE_FORMAT(ev.duree, '%Hh%i') as duree, usr.prenom, et.nom AS type, ev.rep_periodique,
		DATE_FORMAT(ev.ev_date,'%d/%m/%Y %Hh%i') as treated_ev_date,
		DATE_FORMAT(ev.ev_date,'%Y%m%d%Hh%i') as filter_ev_date,
		ev.type_id, ev.tache_terminee, ev.rep_parent_id
		FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut, " . TB . "utilisateurs usr, " . TB . "evenement_types et
		WHERE ev_date >= '" . $date_debut["Y"] . "-" . $date_debut["M"] . "-" . $date_debut["d"] . " 00:00:00'
		AND ev_date <= '" . $date_fin["Y"] . "-" . $date_fin["M"] . "-" . $date_fin["d"] . " 23:59:59'
		AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
		OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
		OR '". $_SESSION['ponderation'] ."' >= 10)
		AND ev.audience = ut.id
		AND ev.utilisateur_id = usr.id
		AND ev.type_id = et.id
		ORDER BY filter_ev_date";

		$title_str = $title_begin . $date_debut["d"] . "/" . $date_debut["M"] . "/" . $date_debut["Y"] .
		" au " . $date_fin["d"] . "/" . $date_fin["M"] . "/" . $date_fin["Y"] ;
	break;
	case "evenement" ;
		/* d&eacute;finition de la query avec v&eacute;rification des droits */
		$sql = "SELECT ev.id , CONCAT( LEFT(ev.titre, 40) ,  '...'  )  AS cut_titre,  usr.username,
		DATE_FORMAT(ev.duree, '%Hh%i') as duree, usr.prenom, et.nom AS type, ev.rep_periodique,
		DATE_FORMAT(ev.ev_date,'%d/%m/%Y %Hh%i') as treated_ev_date,
		DATE_FORMAT(ev.ev_date,'%Y%m%d%Hh%i') as filter_ev_date,
		ev.type_id, ev.tache_terminee, ev.rep_parent_id
		FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut, " . TB . "utilisateurs usr, " . TB . "evenement_types et
		WHERE ev_date >= '" . $date_debut["Y"] . "-" . $date_debut["M"] . "-" . $date_debut["d"] . " 00:00:00'
		AND ev_date <= '" . $date_fin["Y"] . "-" . $date_fin["M"] . "-" . $date_fin["d"] . " 23:59:59'
		AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
		OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
		OR '". $_SESSION['ponderation'] ."' >= 10)
		AND ev.audience = ut.id
		AND ev.utilisateur_id = usr.id
		AND ev.type_id = et.id
		AND ev.type_id != 4
		ORDER BY filter_ev_date";

		$title_str = $title_begin . $date_debut["d"] . "/" . $date_debut["M"] . "/" . $date_debut["Y"] .
		" au " . $date_fin["d"] . "/" . $date_fin["M"] . "/" . $date_fin["Y"] ;
	break;
	case "tache" ;
		/* d&eacute;finition de la query avec v&eacute;rification des droits */
		$sql = "SELECT ev.id , CONCAT( LEFT(ev.titre, 40) ,  '...'  )  AS cut_titre,  usr.username,
		DATE_FORMAT(ev.duree, '%Hh%i') as duree, usr.prenom, et.nom AS type, ev.rep_periodique,
		DATE_FORMAT(ev.ev_date,'%d/%m/%Y %Hh%i') as treated_ev_date,
		DATE_FORMAT(ev.ev_date,'%Y%m%d%Hh%i') as filter_ev_date,
		ev.type_id, ev.tache_terminee, ev.rep_parent_id
		FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut, " . TB . "utilisateurs usr, " . TB . "evenement_types et
		WHERE ev_date >= '" . $date_debut["Y"] . "-" . $date_debut["M"] . "-" . $date_debut["d"] . " 00:00:00'
		AND ev_date <= '" . $date_fin["Y"] . "-" . $date_fin["M"] . "-" . $date_fin["d"] . " 23:59:59'
		AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
		OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
		OR '". $_SESSION['ponderation'] ."' >= 10)
		AND ev.audience = ut.id
		AND ev.utilisateur_id = usr.id
		AND ev.type_id = et.id
		AND ev.type_id = 4
		ORDER BY filter_ev_date";

		$title_str = $title_begin . $date_debut["d"] . "/" . $date_debut["M"] . "/" . $date_debut["Y"] .
		" au " . $date_fin["d"] . "/" . $date_fin["M"] . "/" . $date_fin["Y"] ;

	break;
	case "prive" ;
		/* d&eacute;finition de la query avec v&eacute;rification des droits */
		$sql = "SELECT ev.id , CONCAT( LEFT(ev.titre, 40) ,  '...'  )  AS cut_titre,  usr.username,
		DATE_FORMAT(ev.duree, '%Hh%i') as duree, usr.prenom, et.nom AS type, ev.rep_periodique,
		DATE_FORMAT(ev.ev_date,'%d/%m/%Y %Hh%i') as treated_ev_date,
		DATE_FORMAT(ev.ev_date,'%Y%m%d%Hh%i') as filter_ev_date,
		ev.type_id, ev.tache_terminee, ev.rep_parent_id
		FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut, " . TB . "utilisateurs usr, " . TB . "evenement_types et
		WHERE ev_date >= '" . $date_debut["Y"] . "-" . $date_debut["M"] . "-" . $date_debut["d"] . " 00:00:00'
		AND ev_date <= '" . $date_fin["Y"] . "-" . $date_fin["M"] . "-" . $date_fin["d"] . " 23:59:59'
		AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
		OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
		OR '". $_SESSION['ponderation'] ."' >= 10)
		AND ev.audience = ut.id
		AND ev.utilisateur_id = usr.id
		AND ev.type_id = et.id
		AND ev.prive = 1
		ORDER BY filter_ev_date";

		$title_str = $title_begin . $date_debut["d"] . "/" . $date_debut["M"] . "/" . $date_debut["Y"] .
		" au " . $date_fin["d"] . "/" . $date_fin["M"] . "/" . $date_fin["Y"] ;
	break;
	}

	/* on execute la query */
	$result = $db->query($sql);

	/* si r&eacute;sutls */
	if ( DB::isError( $result = $db->query( $sql ) ) ) {
	    // on fait rien
	}else{
	    /*******************
	        on print le titre du tableau
	    */
		?>
		<div style="text-align: center;">
		<table style="width: 100%; text-align: center; margin-left: 0px; margin-right: auto;" border="0" cellpadding="2" cellspacing="2">
		<tbody>
		<tr>
		<td style="vertical-align: top; background-color: rgb(100, 100, 100);">
		<span style="font-family: georgia"><big style="font-weight: bold; color: rgb(220, 220, 220);">
		<?php echo $title_str; ?></big></span></td>
		</tr>
		</tbody>
		</table>

		<div class="info">
		<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
		<tbody>
		<tr>
			<th><big>Type</big></td>
			<th><big>Date</big></td>
			<th><big>Dur&eacute;e</big></td>
			<th><big>Titre</big></td>
			<th><big>Pseudo</big></td>
			<th><big>Action</big></td>
		</tr>

		<?php /* maintenant on peut printer les donn&eacute;es */
		while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
		?>

		<tr>
			<td ><?php echo  $row["type"]; ?></td>
			<td ><?php echo  $row["treated_ev_date"]; ?></td>
			<td ><?php echo  $row["duree"]; ?></td>
			<td >

				<?php if ( $row["type_id"] == 4 ){
				if ( $row["tache_terminee"] == 0 ) {
					echo IMG_CAL_1;
				} else {
					echo IMG_CAL_2;
				}}

				if ( $row["rep_periodique"] == 1 ){
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"] . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=$row["cut_titre"];
					print_url ($url, $label);
					echo IMG_CAL_5;
				 }else{
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"] . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=$row["cut_titre"];
					print_url ($url, $label);
				 }?>

			</td>

			<td><?php echo  $row["username"]; ?></td>
			<td>

			<?php if ( $has_rights != "no" ){
			if ( $row["rep_periodique"] == 1 and  $row["rep_parent_id"] != null ){
				$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["rep_parent_id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
				$label=IMG_CAL_6;
				print_url($url,$label);
			}else{
				$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
				$label=IMG_CAL_6;
				print_url($url,$label);
			}}

			if ( $has_rights != "no" ){
			if ( $row["rep_periodique"] == 1 ){
				if ( $row["rep_parent_id"] == null ) { // ceci est l'&eacute;venement parent !! on efface tout !!!
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1001" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=MSG_CAL_4;
					print_del_url($url, $label);
				} else {
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1000" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=MSG_CAL_4;
					print_del_url($url, $label);
				}
			}else{
				$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1000" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
				$label=MSG_CAL_5;
				print_del_url($url, $label);
			}

			$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=5" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
			$label=IMG_CONT_5;
			print_url($url,$label);
			}?>
			</td>
	    <?php } //while

	    /**********************
	        fin du tableau
	    */
	    ?>
	    </tbody>
	    </table>
		</div>
		<br/> <?php } // if

	$url=INDEX_FORM . '?id=' . $service_id . '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
	$label=MSG_CAL_3;
	print_url($url,$label);
	echo "  ";
	$url=INDEX_FORM . '?id=' . $service_id . "&cal_mode=6". '&n_day=' . $n_day . '&n_month=' . $n_month . '&n_year=' . $n_year;
	$label="Imprimer";
	print_url($url,$label);



}
/*
** Function : translate_month
**
** Deal : translation du num&eacute;ro de mois en francais
**
** Params : month => num&eacute;ro 1-12 ou texte anglais
*/
function translate_month ( $month )
{

	switch ($month)
	{
	case "January" ;
	   $fr_mois="Janvier";
	   break;
	case "February" ;
	   $fr_mois="F&eacute;vrier";
	   break;
	case "March" ;
	   $fr_mois="Mars";
	   break;
	case "April" ;
	   $fr_mois="Avril";
	   break;
	case "May" ;
	   $fr_mois="Mai";
	   break;
	case "June" ;
	   $fr_mois="Juin";
	   break;
	case "July" ;
	   $fr_mois="Juillet";
	   break;
	case "August" ;
	   $fr_mois="Aout";
	   break;
	case "September" ;
	   $fr_mois="Septembre";
	   break;
	case "October" ;
	   $fr_mois="Octobre";
	   break;
	case "November" ;
	   $fr_mois="Novembre";
	   break;
	case "December" ;
	   $fr_mois="D&eacute;cembre";
	   break;
	case "1" ;
	   $fr_mois="Janvier";
	   break;
	case "2" ;
	   $fr_mois="F&eacute;vrier";
	   break;
	case "3" ;
	   $fr_mois="Mars";
	   break;
	case "4" ;
	   $fr_mois="Avril";
	   break;
	case "5" ;
	   $fr_mois="Mai";
	   break;
	case "6" ;
	   $fr_mois="Juin";
	   break;
	case "7" ;
	   $fr_mois="Juillet";
	   break;
	case "8" ;
	   $fr_mois="Aout";
	   break;
	case "9" ;
	   $fr_mois="Septembre";
	   break;
	case "10" ;
	   $fr_mois="Octobre";
	   break;
	case "11" ;
	   $fr_mois="Novembre";
	   break;
	case "12" ;
	   $fr_mois="D&eacute;cembre";
	   break;
	}

	return $fr_mois;
}

/*
** Function : print_cal_header
**
** Deal : cr&eacute;ation du header du calendrier
**
** Params : database connection, based form name, service id, dates.
*/
function print_cal_header($db,  $service_id, $n_day, $n_month, $n_year)
{
    /* initialisation de la date en fonction des parametres */
	$today_month = translate_month ($n_month);
	$today_day = $n_day;
	$today_year = $n_year;

	/* special treatment fo dates */
	if ( $n_month == 1 ){
	    $n_month_minus=12;
	    $n_year_minus=$n_year - 1;
	}else{
	    $n_month_minus=$n_month - 1;
	    $n_year_minus=$n_year;
	}
	if ( $n_month == 12 ){
	    $n_month_plus=1;
	    $n_year_plus=$n_year + 1;
	}else{
	    $n_month_plus=$n_month + 1;
	    $n_year_plus=$n_year;
	}

    /****************************
    	Print du header
    */
	?>
	<div class="cal_header">
	<table border="0" cellpadding="2" cellspacing="2">
	<tbody>
	<tr>
	<td >
		<center>
		
		<?php
		
		$url=INDEX_FORM . "?id=" . $service_id . "&cal_action=1&n_day=" . $n_day . "&n_month=" . $n_month_minus . "&n_year=" . $n_year_minus;
		$label=IMG_CAL_7;
		print_url($url,$label);
		?>


		<span><?php echo  $today_month . " " . $today_year ;?></span>
		<?php
		$url=INDEX_FORM . "?id=" . $service_id . "&cal_action=1&n_day=" . $n_day . "&n_month=" . $n_month_plus . "&n_year=" . $n_year_plus;
		$label=IMG_CAL_8;
		print_url($url,$label);
		?>
		</center>
	</td>
	</tr>
	</tbody>
	</table>
	</div>


	<table class="cal_month_view" width="100%" border="0" cellpadding="2" cellspacing="2">
	<tbody>
	<th width="14%">Lundi</th>
	<th width="14%">Mardi</th>
	<th width="14%">Mercredi</th>
	<th width="14%">Jeudi</th>
	<th width="14%">Vendredi</th>
	<th width="14%">Samedi</th>
	<th width="13%">Dimanche</th>
	<?php }

/*
** Function : print_cal_content
**
** Deal : creation du contenu du calendrier
**
** Params : database connection, based form name, service id, dates.
*/
function print_cal_content($db,  $service_id, $n_day, $n_month, $n_year)
{


	/* lecture des variable */
/*
	$n_day = $_GET['n_day'];
	$n_month = $_GET['n_month'];
	$n_year = $_GET['n_year'];
*/
	/* check des droits */
	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";


	/* initilisation d'un tableau avec les jours du mois indexes */
	$Month = new Calendar_Month_Weekdays($n_year, $n_month);
	$Month->build();

	/* pour chaque jour du mois */
	while ($Day = $Month->fetch()) {
	    if ($Day->isFirst()) {
	        echo "<tr>\n";
	    }
	    if ($Day->isEmpty()) {
	        echo "<td>";
	        // on affiche rien en fait !!
	        echo "</td>\n";
	    } else {
	        // on appelle la fonction print_cal_cell pour chaque jours
	        echo '<td style="vertical-align: top; background-color: rgb(209, 209, 209);">';
	        print_cal_cell($db,  $service_id, $Day->thisDay(), $n_month, $n_year);
	        echo "</td>\n";
	    }
	    if ($Day->isLast()) {
	        echo "</tr>\n";
	    }
	}

    /***********************
     	Bas de case
    */
	/* date du jour */
	$today = getdate();
	$today_month =  $today['mon'];
	$today_day = $today['mday'];
	$today_year = $today['year'];

	?>
	</tbody>
	</table>
	<br/>
	<br/>
	<div style="text-align: left;">

	 <?php if ( $has_rights != "no" ){
		$url=INDEX_FORM . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=NA"  . "&n_day=" . $today_day . "&n_month=" . $today_month . "&n_year=" . $today_year . "&edit=1";
		$label=MSG_CAL_6;
		print_url($url,$label);
	 	}
		echo "<br/>";
		$url=INDEX_FORM . "?id=" . $service_id . "&cal_action=calendar&n_day=" . $today_day . "&n_month=" . $today_month . "&n_year=" . $today_year;
		$label=MSG_CAL_7;
		print_url($url,$label);
		echo "<br/>";

// TODO
$url=INDEX_FORM . "?id=" . $service_id . "&cal_mode=1111&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
$label=MSG_CAL_8;
print_url($url, $label);


	?>
	<br/>
	</div>

	<div style="text-align: center;">
	<br/>
	<table style="width: 100%; text-align: center; margin-left: 0px; margin-right: auto;" border="0" cellpadding="2" cellspacing="2">
	<tbody>
	<tr>
	<td style="vertical-align: top; background-color: rgb(100, 100, 100);">
	<span style="font-family: georgia"><big style="font-weight: bold; color: rgb(220, 220, 220);">
	Affichage par listes </big></span></td>
	</tr>
	</tbody>
	</table>
	<?php /* formulaire de filtrage */
	$form = new HTML_QuickForm('frmLogin', 'post', INDEX_FORM . "?id=" . $service_id . "&cal_mode=2&cal_action=calendar&n_day=" . $today_day . "&n_month=" . $today_month . "&n_year=" . $today_year);
	$options = array(
		'language'  => 'fr',
		'format'    => 'dMY',
		'minYear'   => 2004,
		'maxYear'   => 2010
		);
	$form->addElement('date', 'date_debut', "Depuis :", $options);
	$form->setDefaults(array('date_debut' => $n_year . "-" . $n_month . "-" . $n_day));

	$form->addElement('date', 'date_fin', "Jusqu'a :", $options);
	$form->setDefaults(array('date_fin' => $n_year + 1 . "-" . $n_month . "-" . $n_day));

	$list_values = array(
		'tout'  => 'Tout',
		'evenement'    => 'Les evenements',
		'tache'   => 'Les t&acirc;ches',
		'prive'   => 'Les elements prives'
		);

	$form->addElement('select', 'type_query', "Montrer :", $list_values);
	$form->addElement('submit', 'bouton_effacer', 'Lancer');
	form_display($form, "Formulaire de filtrage", "forms", "100%");

	?></div><?php
}



/*
** Function : print_cal_footer
**
** Deal : print du footer du calendrier
**
*/
function print_cal_footer()
{

	/* initialisation de la date du jour */
	$today = getdate();
	$today_month = translate_month ( $today['month'] );
	$today_day = $today['mday'];
	$today_year = $today['year'];

	/****************
	    print
	*/
	?>
	<div style="text-align: right;"><small><span
	style="font-family: georgia; font-style: italic;">Nous sommes le : <?php echo $today_day . " " .  $today_month . " " . $today_year; ?> </span></small><br/>
	</div>
	<?php }

/*
** Function : print_cal_cell
**
** Deal : print de chaque case du calendrier
**
** Params : database connection, based form name, service id, dates.
*/
function print_cal_cell($db,  $service_id, $n_day, $n_month, $n_year)
{

	/* check des droits */
	if ($_SESSION['ponderation'] < 10)
		$has_rights = "no";
	else
		$has_rights = "yes";


	/* initialisation de la date du jour */
	$today = getdate();
	$today_month = $today['mon'];
	$today_day = $today['mday'];
	$today_year = $today['year'];


	if ($today_day == $n_day and $today_month == $n_month and $today_year == $n_year){
		$today_color="rgb(182,213,234)";
	}else{
		$today_color="rgb(224, 224, 224)";
	}

	/* definition de la query avec verification des droits */
	$sql = "SELECT ev.* , CONCAT( LEFT( ev.titre, 15  ) ,  '...'  )  AS cut_titre,
    DATE_FORMAT(ev_date, '%H') as heure, DATE_FORMAT(ev_date, '%i') as minute
	FROM " . TB . "evenements ev, " . TB . "utilisateur_types ut
	WHERE ev_date >= '" . $n_year . "-" . $n_month . "-" . $n_day . " 00:00:00'
	AND ev_date <= '" . $n_year . "-" . $n_month . "-" . $n_day . " 23:59:59'
	AND ( ( ev.prive = 1 AND ev.utilisateur_id = " . $_SESSION['user_id'] . " )
	OR ( ev.prive = 0 AND ut.ponderation <= " . $_SESSION['ponderation'] ." )
	OR '". $_SESSION['ponderation'] ."' >= 10)
	AND ev.audience = ut.id
	ORDER BY heure, minute";

	/* on execute la query */
	$result = $db->query($sql);

	/* si resutls */
	if ( DB::isError( $result = $db->query( $sql ) ) ) {
	    // on fait rien
	}else{
	    /*******************
	        on print le debut de la case (tableau HTML)
	    */
	    ?>
	    <table style="text-align: left; width: 100%; height: 100%;" border="0" cellpadding="1" cellspacing="1">
	    <tbody>
	    <tr>
	    <td style="vertical-align: top; background-color: <?php echo $today_color; ?>;"><?php echo $n_day ; ?><br/>
	    <table style="text-align: left; width: 100%; " border="0" cellpadding="0" cellspacing="0">
	    <tbody>
	    <?php /*********************
	    	 on remplit la table
	    */
	    while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
		    ?>
	        <tr>
	        <td style="vertical-align: top; background-color: rgb(255, 255, 255);"><small><small><?php echo  $row["heure"] . "h" . $row["minute"]; ?></small></small><br/>
	        </td>
	        <td style="vertical-align: top; background-color: rgb(255, 255, 255);">
				<?php

				if ( $row["type_id"] == 4 ){
				if ( $row["tache_terminee"] == 0 ) {
					echo IMG_CAL_1;
				} else {
					echo IMG_CAL_2;
				}}

				if ( $row["rep_periodique"] == 1 ){
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"] . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label="<small>" . $row["cut_titre"]. "</small>";
					print_url($url,$label);
					echo IMG_CAL_5;
	         	}else{
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"] . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label="<small>" . $row["cut_titre"] . "</small>";
					print_url($url,$label);
	            }

				?>

	        </td>
	        <td style="vertical-align: top; text-align: right; background-color: rgb(255, 255, 255);">

			<?php


			if ( $has_rights != "no" ){
			   if ( $row["rep_periodique"] == 1 and  $row["rep_parent_id"] != null ){
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["rep_parent_id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
					$label=IMG_CAL_6;
					print_url($url,$label);
	        	 }else{
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year . "&edit=1";
					$label=IMG_CAL_6;
					print_url($url,$label);
	        	 }}

			 if ( $has_rights != "no" ){
				if ( $row["rep_periodique"] == 1 ){
				if ( $row["rep_parent_id"] == null ) { // ceci est l'evenement parent !! on efface tout !!!
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1001" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=MSG_CAL_4;
					print_del_url($url, $label);

				} else {
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1000" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=MSG_CAL_4;
					print_del_url($url, $label);
				}
				}else{
					$url=INDEX_FORM  . "?id=" . $service_id . "&cal_mode=1000" . "&cal_id=" . $row["id"]  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year;
					$label=MSG_CAL_5;
					print_del_url($url, $label);
	        	}

				}



				?>
	        </td>
	        </tr>
		    <?php } //while

	    /**********************
	        fin du tableau
	    */
	    ?>
	    </tbody>
	    </table>
	    <?php } // if

    /*********************
    	en bas de la case
    */
	?>
	<p>
	<?php if ( $has_rights != "no" ){

		$url=INDEX_FORM . "?id=" . $service_id . "&cal_mode=0" . "&cal_id=NA"  . "&n_day=" . $n_day . "&n_month=" . $n_month . "&n_year=" . $n_year  . "&edit=1";
		$label=IMG_CAL_9;
		print_url($url, $label);

	}
	?>
	</table>
	</p>
	<?php
}